//
//  WDWorkOrderSearchViewModel.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/19.
//

#import "WDWorkOrderSearchViewModel.h"

@implementation WDWorkOrderSearchViewModel

- (void)initialize {
    [super initialize];
    
    self.title = @"搜索";
    
    _markets = [NSMutableArray new];
    
    _reqModel = [WDStationTaskSearchReqModel new];
    _reqModel.current = 1;
    _reqModel.size = 10;
    _reqModel.stationId = @([EDSAppDelegateTool sharedInstance].userInfoModel.stationId);
}

- (void)setupData {
    id historyList = [EDSUtility readCacheDataLocalKey:kCacheMainSearchKeyWordsData saveFilename:kCacheMainSearchKeyWordsFileName];
    if (historyList)
    {
        [self.markets removeAllObjects];
        [self.markets addObjectsFromArray:historyList];
    }
}

/// 清空历史记录
- (void)clearHistoryRecord
{
    [self.markets removeAllObjects];
    [EDSUtility writeCacheDataLocalKey:kCacheMainSearchKeyWordsData saveFilename:kCacheMainSearchKeyWordsFileName saveid:self.markets];
}

/// 更新搜索关键字
/// @param keyWord keyWord description
- (void)updateWithSearchKeyWord:(NSString *)keyWord
{
    if ([objectNull(keyWord) isEqualToString:@""])
    {
        return;
    }
    BOOL ret = NO;
    WDHistorySearchWordsModel *exitModel = nil;
    for (WDHistorySearchWordsModel *ent in self.markets)
    {
        if ([ent.keyWord isEqualToString:keyWord])
        {
            ent.num += 1;
            ent.dateTime = [[NSDate date] stringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
            ret = YES;
            exitModel = ent;
            [self.markets removeObject:ent];
            break;
        }
    }
    
    if (!ret)
    {
//        NSLog(@"market=%@", self.markets);
        WDHistorySearchWordsModel *model = [WDHistorySearchWordsModel new];
        model.keyWord = keyWord;
        model.dateTime = [[NSDate date] stringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
        model.num = 1;
        [self.markets insertObject:model atIndex:0];
    }
    else
    {
        if (exitModel)
        {
            [self.markets insertObject:exitModel atIndex:0];
        }
    }
    
    NSArray *tmpList = [self.markets copy];
    if (tmpList.count > 9)
    {// 当出现大雨9个的时候，就只要9个
        [self.markets removeAllObjects];
        [self.markets addObjectsFromArray:[tmpList subarrayWithRange:NSMakeRange(0, 9)]];
    }
    
    [EDSUtility writeCacheDataLocalKey:kCacheMainSearchKeyWordsData saveFilename:kCacheMainSearchKeyWordsFileName saveid:self.markets];
}


/// 请求数据
/// - Parameter completion: completion description
- (void)requestWithTaskSearch:(RequestCompletionBlock)completion {
    _reqModel.stationId = @([EDSAppDelegateTool sharedInstance].userInfoModel.stationId);
//    _reqModel.engineerId = @([EDSAppDelegateTool sharedInstance].userInfoModel.id);
    @weakify(self);
    WDStationTaskSearchRequest *request = [[WDStationTaskSearchRequest alloc] initWithModel:self.reqModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            if (self.reqModel.current == 1) {
                [self.dataList  removeAllObjects];
            }
            NSArray *list = respond.data;
            [self.dataList addObjectsFromArray:list];
            if (list && list.count > 0) {
                self.reqModel.current += 1;
            }
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
